﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Utils;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmPhieuKeToan : Form
    {
        private PhieuKeToanManagement phieuketoanMgt = new PhieuKeToanManagement();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private NhanVienMgt nhanvienMgt = new NhanVienMgt();
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private MucChiMgt mucchiMgt = new MucChiMgt();
        private BangMaPhieuMgt bangmaphieuMgt = new BangMaPhieuMgt();

        private PhieuKeToan phieuketoan = new PhieuKeToan();
        private bool IsEdit = false;
        private int idLoaiPhieu = 10;

        private DateTime dtOld = new DateTime();
        private string prefixOld = "";

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmPhieuKeToan(NhanVien nhanvien)
        {
            InitializeComponent();

            this.nhanvien = nhanvien;
            txtNguoiLap.Text = this.nhanvien.HoTen;
            BindingToCbo();
            ChangeControlStatus(false);
        }

        public FrmPhieuKeToan(PhieuKeToan phieuketoan)
        {
            InitializeComponent();

            BindingToCbo();
            ChangeControlStatus(false);
            IsEdit = true;

            this.phieuketoan = phieuketoan;
            lblId.Text = phieuketoan.Id.ToString();

            FillDataBound();
        }

        private void dgvNo_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {            
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex == 0)
                {
                    if (e.RowIndex > 0)
                    {
                        if (dgvCo.Rows.Count - 1 <= 1)
                        {
                            string sMaSoTK = dgvNo.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
                            dgvNo.Rows[e.RowIndex].Cells[1].Value = dgvNo.Rows[e.RowIndex].Cells[0].Value;
                        }
                        else
                        {
                            MessageBox.Show("Đã có một tài khoản Có");
                            dgvNo.Rows.RemoveAt(e.RowIndex);
                        }
                    }
                    else
                    {
                        string sMaSoTK = dgvNo.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
                        dgvNo.Rows[e.RowIndex].Cells[1].Value = dgvNo.Rows[e.RowIndex].Cells[0].Value;
                    }
                }
                else if (e.ColumnIndex == 2)
                {
                    string sLoaiDT = dgvNo.Rows[e.RowIndex].Cells[2].FormattedValue.ToString();
                    switch (sLoaiDT)
                    {
                        case "Khách hàng":
                            cboDoiTuongNo.DataSource = null;
                            cboDoiTuongNo.DataSource = GetAllKhachHang();
                            cboDoiTuongNo.DisplayMember = "Ten";
                            cboDoiTuongNo.ValueMember = "Id";
                            break;
                        case "Nhân viên":
                            cboDoiTuongNo.DataSource = null;
                            cboDoiTuongNo.DataSource = GetAllNhanVien();
                            cboDoiTuongNo.DisplayMember = "HoTen";
                            cboDoiTuongNo.ValueMember = "Id";
                            break;
                        case "Nhà cung cấp":
                            cboDoiTuongNo.DataSource = null;
                            cboDoiTuongNo.DataSource = GetAllNhaCungCap();
                            cboDoiTuongNo.DisplayMember = "TenNhaCungCap";
                            cboDoiTuongNo.ValueMember = "Id";
                            break;
                        case "Nội bộ":
                            cboDoiTuongNo.DataSource = null;
                            cboDoiTuongNo.DataSource = GetAllChiNhanhCongTy();
                            cboDoiTuongNo.DisplayMember = "TenChiNhanh";
                            cboDoiTuongNo.ValueMember = "Id";
                            break;
                        default:
                            cboDoiTuongNo.DataSource = null;
                            break;
                    }
                }
            }
        }

        private void dgvCo_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex == 0)
                {
                    if (e.RowIndex > 0)
                    {
                        if (dgvNo.Rows.Count - 1 <= 1)
                        {
                            string sMaSoTK = dgvCo.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
                            dgvCo.Rows[e.RowIndex].Cells[1].Value = dgvCo.Rows[e.RowIndex].Cells[0].Value;
                        }
                        else
                        {
                            MessageBox.Show("Đã có một tài khoản Nợ");
                            dgvCo.Rows.RemoveAt(e.RowIndex);
                        }
                    }
                    else
                    {
                        string sMaSoTK = dgvCo.Rows[e.RowIndex].Cells[0].FormattedValue.ToString();
                        dgvCo.Rows[e.RowIndex].Cells[1].Value = dgvCo.Rows[e.RowIndex].Cells[0].Value;
                    }
                }
                else if (e.ColumnIndex == 2)
                {
                    string sLoaiDT = dgvCo.Rows[e.RowIndex].Cells[2].FormattedValue.ToString();
                    switch (sLoaiDT)
                    {
                        case "Khách hàng":
                            cboDoiTuongCo.DataSource = null;
                            cboDoiTuongCo.DataSource = GetAllKhachHang();
                            cboDoiTuongCo.DisplayMember = "Ten";
                            cboDoiTuongCo.ValueMember = "Id";
                            break;
                        case "Nhân viên":
                            cboDoiTuongCo.DataSource = null;
                            cboDoiTuongCo.DataSource = GetAllNhanVien();
                            cboDoiTuongCo.DisplayMember = "HoTen";
                            cboDoiTuongCo.ValueMember = "Id";
                            break;
                        case "Nhà cung cấp":
                            cboDoiTuongCo.DataSource = null;
                            cboDoiTuongCo.DataSource = GetAllNhaCungCap();
                            cboDoiTuongCo.DisplayMember = "TenNhaCungCap";
                            cboDoiTuongCo.ValueMember = "Id";
                            break;
                        case "Nội bộ":
                            cboDoiTuongCo.DataSource = null;
                            cboDoiTuongCo.DataSource = GetAllChiNhanhCongTy();
                            cboDoiTuongCo.DisplayMember = "TenChiNhanh";
                            cboDoiTuongCo.ValueMember = "Id";
                            break;
                        default:
                            cboDoiTuongCo.DataSource = null;
                            break;
                    }
                }
            }
        }

        private void dgvNo_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }

        private void dgvCo_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }

        private void cboChiNhanhCTy_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy b in cboChiNhanhCTy.Items)
            {
                if (b.TenChiNhanh.ToLower().Contains(cboChiNhanhCTy.Text) && cboChiNhanhCTy.Text != "")
                {
                    cboChiNhanhCTy.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboChiNhanhCTy.SelectedValue == null)
            {
                cboChiNhanhCTy.Text = "";
            }
        } 

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ChangeControlStatus(true);
                        
            txtNoiDung.Text = "";
            txtKemTheo.Text = "";

            prefixOld = "";
            dtOld = new DateTime();

            IsEdit = false;
            cboMucChi.SelectedIndex = 0;

            dgvNo.Rows.Clear();
            dgvCo.Rows.Clear();

            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnGhi.Enabled = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            IsEdit = true;
            ChangeControlStatus(true);

            btnSua.Enabled = false;
            btnXoa.Enabled = false;
            btnGhi.Enabled = true;
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            if (dgvCo.Rows.Count <= 1 || dgvNo.Rows.Count <= 1)
            {
                MessageBox.Show("Chưa nhập đầy đủ dữ liệu");
                return;
            }
            else
            {
                ResultObj resultObj = new ResultObj();

                phieuketoan.Prefix = cboPrefix.Text;
                phieuketoan.OrderNumber = txtMaSoChungTu.Text;
                phieuketoan.MaSo = cboPrefix.Text + "/" + dtpNgayLap.Value.ToString("MM") + dtpNgayLap.Value.ToString("yy") + "/" + txtMaSoChungTu.Text;
                phieuketoan.History = "";
                phieuketoan.NgayThang = dtpNgayLap.Value;
                phieuketoan.NguoiLapPhieu = txtNguoiLap.Text;
                phieuketoan.NoiDung = (cboMucChi.Text == "") ? txtNoiDung.Text : cboMucChi.Text + "_" + txtNoiDung.Text;
                phieuketoan.ChungTuKemTheo = txtKemTheo.Text;

                ChiNhanhCongTy chinhanhcty = (ChiNhanhCongTy)cboChiNhanhCTy.SelectedItem;
                phieuketoan.IdChiNhanhCongTy = chinhanhcty.Id;

                if (dgvNo.CurrentRow.Index >= 0 && dgvCo.CurrentRow.Index >= 0)
                {
                    if (IsEdit)
                    {
                        phieuketoan.Id = Convert.ToInt32(lblId.Text);
                        resultObj = phieuketoanMgt.UpdatePhieuKeToan(phieuketoan);

                        EditOrRemoveDinhKhoan(phieuketoan.MaSo);
                        DinhKhoan(chinhanhcty.TenChiNhanh);
                    }
                    else
                    {
                        resultObj = phieuketoanMgt.SaveNewPhieuKeToan(phieuketoan);
                        phieuketoan.Id = resultObj.ReturnCode;

                        DinhKhoan(chinhanhcty.TenChiNhanh);
                    }
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn Tài khoản Nợ/Có. Vui lòng kiểm tra lại!");
                }


                if (resultObj.ReturnCode > 0)
                {
                    MessageBox.Show("Lưu thành công", "Thông báo");

                    IsEdit = false;
                    ChangeControlStatus(false);

                    lblId.Text = resultObj.ReturnCode.ToString();

                    btnSua.Enabled = true;
                    btnXoa.Enabled = true;
                    btnGhi.Enabled = false;
                }
                else if (resultObj.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    resultObj = phieuketoanMgt.DeletePhieuKeToan(phieuketoan);
                }
                else
                {
                    MessageBox.Show("Thất bại", "Thông báo");
                    resultObj = phieuketoanMgt.DeletePhieuKeToan(phieuketoan);
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Bạn có chắc chắn muốn xóa chứng từ này không?", "Xóa phiếu kế toán", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                ResultObj resultObj = new ResultObj();

                PhieuKeToan phieuketoan = new PhieuKeToan();
                phieuketoan.MaSo = txtMaSoChungTu.Text;
                phieuketoan.NgayThang = dtpNgayLap.Value;
                phieuketoan.NguoiLapPhieu = txtNguoiLap.Text;
                phieuketoan.NoiDung = (cboMucChi.Text == "") ? txtNoiDung.Text : cboMucChi.Text + "_" + txtNoiDung.Text;
                phieuketoan.ChungTuKemTheo = txtKemTheo.Text;

                ChiNhanhCongTy chinhanhcty = (ChiNhanhCongTy)cboChiNhanhCTy.SelectedItem;
                phieuketoan.IdChiNhanhCongTy = chinhanhcty.Id;

                phieuketoan.Id = Convert.ToInt32(lblId.Text);
                phieuketoan.IsDelete = 1;

                resultObj = phieuketoanMgt.UpdatePhieuKeToan(phieuketoan);
                EditOrRemoveDinhKhoan(phieuketoan.MaSo);

                MessageBox.Show("Đã xóa", "Thông báo!");

                btnXoa.Enabled = false;
                btnSua.Enabled = false;
                btnGhi.Enabled = false;
                ChangeControlStatus(false);
            }
        }  

        private void btnIn_Click(object sender, EventArgs e)
        {

        }

        public void DinhKhoan(string sTenChiNhanhCty)
        {
            ResultObj resultObj = new ResultObj();

            if (dgvNo.Rows.Count == 2)
            {
                for (int i = 0; i < dgvCo.Rows.Count - 1; i++)
                {
                    int IsFail = 0;

                    ButToan buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgayLap.Value;
                    buttoan.ChiNhanh = sTenChiNhanhCty;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = phieuketoan.MaSo;
                    buttoan.IsEdited = 0;
                    buttoan.GhiNo = 0;

                    if (dgvNo.Rows[0].Cells[3].Value != null)
                        buttoan.MaSoKhachHang = dgvNo.Rows[0].Cells[3].FormattedValue.ToString();
                    else
                        IsFail = 1;

                    if (dgvCo.Rows[i].Cells[0].Value != null)
                        buttoan.TKGoc = dgvCo.Rows[i].Cells[0].FormattedValue.ToString();
                    else
                        IsFail = 1;

                    if (dgvNo.Rows[0].Cells[0].Value != null)
                        buttoan.TKDoiUng = dgvNo.Rows[0].Cells[0].FormattedValue.ToString();
                    else
                        IsFail = 1;

                    if (dgvCo.Rows[0].Cells[4].Value != null)
                        buttoan.GhiCo = Convert.ToInt64(dgvCo.Rows[i].Cells[4].Value);
                    else
                        IsFail = 1;

                    if (IsFail == 0)
                    {
                        resultObj = buttoanMgt.SaveNewButToan(buttoan);

                        buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgayLap.Value;
                        buttoan.ChiNhanh = sTenChiNhanhCty;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = phieuketoan.MaSo;
                        buttoan.IsEdited = 0;
                        buttoan.GhiCo = 0;

                        if (dgvNo.Rows[0].Cells[3].Value != null)
                            buttoan.MaSoKhachHang = dgvNo.Rows[0].Cells[3].FormattedValue.ToString();
                        else
                            IsFail = 1;

                        if (dgvNo.Rows[0].Cells[0].Value != null)
                            buttoan.TKGoc = dgvNo.Rows[0].Cells[0].FormattedValue.ToString();
                        else
                            IsFail = 1;

                        if (dgvCo.Rows[i].Cells[0].Value != null)
                            buttoan.TKDoiUng = dgvCo.Rows[i].Cells[0].FormattedValue.ToString();
                        else
                            IsFail = 1;

                        if (dgvCo.Rows[i].Cells[4].Value != null)
                            buttoan.GhiNo = Convert.ToInt64(dgvCo.Rows[i].Cells[4].Value);
                        else
                            IsFail = 1;

                        if (IsFail == 0)
                        {
                            resultObj = buttoanMgt.SaveNewButToan(buttoan);
                        }
                    }
                    else
                        MessageBox.Show("Bạn chưa nhập đủ các thông tin");
                }
            }
            else if (dgvCo.Rows.Count == 2)
            {
                for (int i = 0; i < dgvNo.Rows.Count - 1; i++)
                {
                    int IsFail = 0;

                    ButToan buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgayLap.Value;
                    buttoan.ChiNhanh = sTenChiNhanhCty;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = phieuketoan.MaSo;
                    buttoan.IsEdited = 0;
                    buttoan.GhiCo = 0;

                    if (dgvCo.Rows[0].Cells[3].Value != null)
                        buttoan.MaSoKhachHang = dgvCo.Rows[0].Cells[3].FormattedValue.ToString();
                    else
                        IsFail = 1;

                    if (dgvNo.Rows[0].Cells[0].Value != null)
                        buttoan.TKGoc = dgvNo.Rows[i].Cells[0].FormattedValue.ToString();
                    else
                        IsFail = 1;

                    if (dgvCo.Rows[0].Cells[0].Value != null)
                        buttoan.TKDoiUng = dgvCo.Rows[0].Cells[0].FormattedValue.ToString();
                    else
                        IsFail = 1;

                    if (dgvNo.Rows[0].Cells[4].Value != null)
                        buttoan.GhiNo = Convert.ToInt64(dgvNo.Rows[i].Cells[4].Value);
                    else
                        IsFail = 1;

                    if (IsFail == 0)
                    {
                        resultObj = buttoanMgt.SaveNewButToan(buttoan);

                        buttoan = new ButToan();
                        buttoan.NgayThang = dtpNgayLap.Value;
                        buttoan.ChiNhanh = sTenChiNhanhCty;
                        buttoan.DienGiai = txtNoiDung.Text;
                        buttoan.SoChungTu = phieuketoan.MaSo;
                        buttoan.IsEdited = 0;
                        buttoan.GhiNo = 0;

                        if (dgvCo.Rows[0].Cells[3].Value != null)
                            buttoan.MaSoKhachHang = dgvCo.Rows[0].Cells[3].FormattedValue.ToString();
                        else
                            IsFail = 1;

                        if (dgvCo.Rows[0].Cells[0].Value != null)
                            buttoan.TKGoc = dgvCo.Rows[0].Cells[0].FormattedValue.ToString();
                        else
                            IsFail = 1;

                        if (dgvNo.Rows[i].Cells[0].Value != null)
                            buttoan.TKDoiUng = dgvNo.Rows[i].Cells[0].FormattedValue.ToString();
                        else
                            IsFail = 1;

                        if (dgvNo.Rows[i].Cells[4].Value != null)
                            buttoan.GhiCo = Convert.ToInt64(dgvNo.Rows[i].Cells[4].Value);
                        else
                            IsFail = 1;

                        if (IsFail == 0)
                        {
                            resultObj = buttoanMgt.SaveNewButToan(buttoan);
                        }
                    }
                    else
                        MessageBox.Show("Bạn chưa nhập đủ các thông tin");
                }
            }
        }

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        public void BindingToCbo()
        {
            cboTKCo.DataSource = GetAllTaiKhoanKeToan();
            cboTKCo.DisplayMember = "MaSoTK";
            cboTKCo.ValueMember = "TenTK";

            cboTKNo.DataSource = GetAllTaiKhoanKeToan();
            cboTKNo.DisplayMember = "MaSoTK";
            cboTKNo.ValueMember = "TenTK";

            cboChiNhanhCTy.DataSource = GetAllChiNhanhCongTy();
            cboChiNhanhCTy.DisplayMember = "TenChiNhanh";
            cboChiNhanhCTy.ValueMember = "Id";

            cboMucChi.DataSource = GetAllMucChi();
            cboMucChi.DisplayMember = "Ten";
            cboMucChi.ValueMember = "Id";

            cboPrefix.DataSource = GetAllBangMaPhieu(idLoaiPhieu);
            cboPrefix.DisplayMember = "MaSo";
            cboPrefix.ValueMember = "Id";
        }

        public void ChangeControlStatus(bool IsDisable)
        {
            gpbThongTin.Enabled = IsDisable;
            gpbNo.Enabled = IsDisable;
            gpbCo.Enabled = IsDisable;
            btnGhi.Enabled = IsDisable;
        }

        public void FillDataBound()
        {
            // Fill data to all control with info from param = phieuketoan
            lblId.Text = this.phieuketoan.Id.ToString();
            dtpNgayLap.Value = this.phieuketoan.NgayThang;
            dtOld = this.phieuketoan.NgayThang;

            prefixOld = this.phieuketoan.Prefix;
            cboPrefix.Text = this.phieuketoan.Prefix;
            txtMaSoChungTu.Text = this.phieuketoan.OrderNumber;

            txtKemTheo.Text = this.phieuketoan.ChungTuKemTheo;
            txtNguoiLap.Text = this.phieuketoan.NguoiLapPhieu;

            int index = this.phieuketoan.NoiDung.IndexOf("_");
            if (index == -1)
                txtNoiDung.Text = this.phieuketoan.NoiDung;
            else
            {
                txtNoiDung.Text = this.phieuketoan.NoiDung.Substring(index + 1, this.phieuketoan.NoiDung.Length - (index + 1));
                cboMucChi.Text = this.phieuketoan.NoiDung.Substring(0, index + 1);
            }

            ChiNhanhCongTy chinhanhcongty = (from cn in GetAllChiNhanhCongTy()
                                             where cn.Id == this.phieuketoan.IdChiNhanhCongTy
                                             select cn).FirstOrDefault();
            cboChiNhanhCTy.SelectedValue = chinhanhcongty.Id;


            // ****************************
            // ** Fill data to datagrids **
            // ****************************
            #region // ** Fill data to datagrids **
            // Get a list of records with param GhiNo
            List<ButToan> lstButToanFirst = (from b in GetAllButToan()
                                             where b.SoChungTu == this.phieuketoan.MaSo && b.GhiNo != 0
                                             select b).ToList();

            // Get a list of records with param GhiCo
            List<ButToan> lstButToanSecond = (from b in GetAllButToan()
                                              where b.SoChungTu == this.phieuketoan.MaSo && b.GhiCo != 0
                                              select b).ToList();

            // Temp = 0 mean only one TKGoc in first list
            // and binding second list to datagrid Co
            if (lstButToanFirst.Count > 0 && lstButToanSecond.Count > 0)
            {
                if (lstButToanFirst.Count == 1 && lstButToanSecond.Count == 1)
                {
                    #region 1N - 1Co
                    // ****************************
                    // ** Binding to datagrid No **
                    // ****************************
                    #region Set value for columns and add to datagrid
                    DataGridViewRow row = new DataGridViewRow();

                    // Column SoHieuTK
                    DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
                    TaiKhoanKeToan tkkt = (from a in GetAllTaiKhoanKeToan()
                                           where a.MaSoTK == lstButToanFirst[0].TKGoc
                                           select a).FirstOrDefault();

                    cboCell.DataSource = GetAllTaiKhoanKeToan();
                    cboCell.DisplayMember = "MaSoTK";
                    cboCell.ValueMember = "TenTK";
                    cboCell.Value = tkkt.TenTK;
                    row.Cells.Add(cboCell);

                    // Column TenTK
                    DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                    cell.Value = tkkt.TenTK;
                    row.Cells.Add(cell);

                    // Column LoaiDoiTuong and DoiTuong
                    List<KhachHang> lstKhachHang = (from c in GetAllKhachHang()
                                                    where c.Ten == lstButToanFirst[0].MaSoKhachHang
                                                    select c).ToList();

                    List<NhaCungCap> lstNhaCungCap = (from s in GetAllNhaCungCap()
                                                      where s.TenNhaCungCap == lstButToanFirst[0].MaSoKhachHang
                                                      select s).ToList();

                    List<NhanVien> lstNhanVien = (from e in GetAllNhanVien()
                                                  where e.HoTen == lstButToanFirst[0].MaSoKhachHang
                                                  select e).ToList();

                    List<ChiNhanhCongTy> lstChiNhanhCongTy = (from p in GetAllChiNhanhCongTy()
                                                              where p.TenChiNhanh == lstButToanFirst[0].MaSoKhachHang
                                                              select p).ToList();

                    DataGridViewComboBoxCell cboCellLoaiDT = new DataGridViewComboBoxCell();
                    DataGridViewComboBoxCell cboCellDT = new DataGridViewComboBoxCell();

                    cboCellLoaiDT.Items.Add("");
                    cboCellLoaiDT.Items.Add("Khách hàng");
                    cboCellLoaiDT.Items.Add("Nhà cung cấp");
                    cboCellLoaiDT.Items.Add("Nhân viên");
                    cboCellLoaiDT.Items.Add("Nội bộ");

                    if (lstKhachHang.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Khách hàng";

                        cboCellDT.DataSource = GetAllKhachHang();
                        cboCellDT.DisplayMember = "Ten";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstKhachHang[0].Id;
                    }
                    else if (lstNhaCungCap.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Nhà cung cấp";

                        cboCellDT.DataSource = GetAllNhaCungCap();
                        cboCellDT.DisplayMember = "TenNhaCungCap";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstNhaCungCap[0].Id;
                    }
                    else if (lstNhanVien.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Nhân viên";

                        cboCellDT.DataSource = GetAllNhanVien();
                        cboCellDT.DisplayMember = "HoTen";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstNhanVien[0].Id;
                    }
                    else if (lstChiNhanhCongTy.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Nội bộ";

                        cboCellDT.DataSource = GetAllChiNhanhCongTy();
                        cboCellDT.DisplayMember = "TenChiNhanh";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstChiNhanhCongTy[0].Id;
                    }

                    row.Cells.Add(cboCellLoaiDT);
                    row.Cells.Add(cboCellDT);

                    // Column SoTien
                    cell = new DataGridViewTextBoxCell();
                    cell.Value = lstButToanFirst[0].GhiNo;
                    row.Cells.Add(cell);

                    // Column Id
                    cell = new DataGridViewTextBoxCell();
                    cell.Value = lstButToanFirst[0].Id;
                    row.Cells.Add(cell);

                    dgvNo.Rows.Add(row);
                    #endregion


                    // ****************************
                    // ** Binding to datagrid Co **
                    // ****************************
                    #region Set value for columns and add to datagrid
                    row = new DataGridViewRow();

                    // Column SoHieuTK
                    cboCell = new DataGridViewComboBoxCell();
                    tkkt = (from a in GetAllTaiKhoanKeToan()
                            where a.MaSoTK == lstButToanSecond[0].TKGoc
                            select a).FirstOrDefault();

                    cboCell.DataSource = GetAllTaiKhoanKeToan();
                    cboCell.DisplayMember = "MaSoTK";
                    cboCell.ValueMember = "TenTK";
                    cboCell.Value = tkkt.TenTK;
                    row.Cells.Add(cboCell);

                    // Column TenTK
                    cell = new DataGridViewTextBoxCell();
                    cell.Value = tkkt.TenTK;
                    row.Cells.Add(cell);

                    // Column LoaiDoiTuong and DoiTuong
                    lstKhachHang = (from c in GetAllKhachHang()
                                    where c.Ten == lstButToanSecond[0].MaSoKhachHang
                                    select c).ToList();

                    lstNhaCungCap = (from s in GetAllNhaCungCap()
                                     where s.TenNhaCungCap == lstButToanSecond[0].MaSoKhachHang
                                     select s).ToList();

                    lstNhanVien = (from e in GetAllNhanVien()
                                   where e.HoTen == lstButToanSecond[0].MaSoKhachHang
                                   select e).ToList();

                    lstChiNhanhCongTy = (from p in GetAllChiNhanhCongTy()
                                         where p.TenChiNhanh == lstButToanSecond[0].MaSoKhachHang
                                         select p).ToList();

                    cboCellLoaiDT = new DataGridViewComboBoxCell();
                    cboCellDT = new DataGridViewComboBoxCell();

                    cboCellLoaiDT.Items.Add("");
                    cboCellLoaiDT.Items.Add("Khách hàng");
                    cboCellLoaiDT.Items.Add("Nhà cung cấp");
                    cboCellLoaiDT.Items.Add("Nhân viên");
                    cboCellLoaiDT.Items.Add("Nội bộ");

                    if (lstKhachHang.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Khách hàng";

                        cboCellDT.DataSource = GetAllKhachHang();
                        cboCellDT.DisplayMember = "Ten";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstKhachHang[0].Id;
                    }
                    else if (lstNhaCungCap.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Nhà cung cấp";

                        cboCellDT.DataSource = GetAllNhaCungCap();
                        cboCellDT.DisplayMember = "TenNhaCungCap";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstNhaCungCap[0].Id;
                    }
                    else if (lstNhanVien.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Nhân viên";

                        cboCellDT.DataSource = GetAllNhanVien();
                        cboCellDT.DisplayMember = "HoTen";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstNhanVien[0].Id;
                    }
                    else if (lstChiNhanhCongTy.Count > 0)
                    {
                        cboCellLoaiDT.Value = "Nội bộ";

                        cboCellDT.DataSource = GetAllChiNhanhCongTy();
                        cboCellDT.DisplayMember = "TenChiNhanh";
                        cboCellDT.ValueMember = "Id";
                        cboCellDT.Value = lstChiNhanhCongTy[0].Id;
                    }

                    row.Cells.Add(cboCellLoaiDT);
                    row.Cells.Add(cboCellDT);

                    // Column SoTien
                    cell = new DataGridViewTextBoxCell();
                    cell.Value = lstButToanSecond[0].GhiCo;
                    row.Cells.Add(cell);

                    // Column Id
                    cell = new DataGridViewTextBoxCell();
                    cell.Value = lstButToanSecond[0].Id;
                    row.Cells.Add(cell);

                    dgvCo.Rows.Add(row);
                    #endregion
                    #endregion
                }
                else
                {
                    #region 1 No - N Co || 1 Co - N No
                    if (lstButToanFirst[0].TKGoc == lstButToanFirst[1].TKGoc && lstButToanFirst[0].TKDoiUng != lstButToanFirst[1].TKDoiUng)
                    {
                        // ****************************
                        // ** Binding to datagrid No **
                        // ****************************
                        #region Set value for columns and add to datagrid
                        DataGridViewRow row = new DataGridViewRow();

                        // Column SoHieuTK
                        DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
                        TaiKhoanKeToan tkkt = (from a in GetAllTaiKhoanKeToan()
                                               where a.MaSoTK == lstButToanFirst[0].TKGoc
                                               select a).FirstOrDefault();

                        cboCell.DataSource = GetAllTaiKhoanKeToan();
                        cboCell.DisplayMember = "MaSoTK";
                        cboCell.ValueMember = "TenTK";
                        cboCell.Value = tkkt.TenTK;
                        row.Cells.Add(cboCell);

                        // Column TenTK
                        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                        cell.Value = tkkt.TenTK;
                        row.Cells.Add(cell);

                        // Column LoaiDoiTuong and DoiTuong
                        List<KhachHang> lstKhachHang = (from c in GetAllKhachHang()
                                                        where c.Ten == lstButToanFirst[0].MaSoKhachHang
                                                        select c).ToList();

                        List<NhaCungCap> lstNhaCungCap = (from s in GetAllNhaCungCap()
                                                          where s.TenNhaCungCap == lstButToanFirst[0].MaSoKhachHang
                                                          select s).ToList();

                        List<NhanVien> lstNhanVien = (from e in GetAllNhanVien()
                                                      where e.HoTen == lstButToanFirst[0].MaSoKhachHang
                                                      select e).ToList();

                        List<ChiNhanhCongTy> lstChiNhanhCongTy = (from p in GetAllChiNhanhCongTy()
                                                                  where p.TenChiNhanh == lstButToanFirst[0].MaSoKhachHang
                                                                  select p).ToList();

                        DataGridViewComboBoxCell cboCellLoaiDT = new DataGridViewComboBoxCell();
                        DataGridViewComboBoxCell cboCellDT = new DataGridViewComboBoxCell();

                        cboCellLoaiDT.Items.Add("");
                        cboCellLoaiDT.Items.Add("Khách hàng");
                        cboCellLoaiDT.Items.Add("Nhà cung cấp");
                        cboCellLoaiDT.Items.Add("Nhân viên");
                        cboCellLoaiDT.Items.Add("Nội bộ");

                        if (lstKhachHang.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Khách hàng";

                            cboCellDT.DataSource = GetAllKhachHang();
                            cboCellDT.DisplayMember = "Ten";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstKhachHang[0].Id;
                        }
                        else if (lstNhaCungCap.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Nhà cung cấp";

                            cboCellDT.DataSource = GetAllNhaCungCap();
                            cboCellDT.DisplayMember = "TenNhaCungCap";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstNhaCungCap[0].Id;
                        }
                        else if (lstNhanVien.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Nhân viên";

                            cboCellDT.DataSource = GetAllNhanVien();
                            cboCellDT.DisplayMember = "HoTen";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstNhanVien[0].Id;
                        }
                        else if (lstChiNhanhCongTy.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Nội bộ";

                            cboCellDT.DataSource = GetAllChiNhanhCongTy();
                            cboCellDT.DisplayMember = "TenChiNhanh";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstChiNhanhCongTy[0].Id;
                        }

                        row.Cells.Add(cboCellLoaiDT);
                        row.Cells.Add(cboCellDT);

                        // Column SoTien
                        cell = new DataGridViewTextBoxCell();

                        long lSoTien = 0;
                        for (int i = 0; i < lstButToanFirst.Count; i++)
                            lSoTien += lstButToanFirst[i].GhiNo;

                        cell.Value = lSoTien;
                        row.Cells.Add(cell);

                        // Column Id
                        cell = new DataGridViewTextBoxCell();
                        cell.Value = lstButToanFirst[0].Id;
                        row.Cells.Add(cell);

                        dgvNo.Rows.Add(row);
                        #endregion


                        // ****************************
                        // ** Binding to datagrid Co **
                        // ****************************
                        #region Set value for columns and add to datagrid
                        for (int i = 0; i < lstButToanSecond.Count; i++)
                        {
                            row = new DataGridViewRow();

                            // Column SoHieuTK
                            cboCell = new DataGridViewComboBoxCell();
                            tkkt = (from a in GetAllTaiKhoanKeToan()
                                    where a.MaSoTK == lstButToanSecond[i].TKGoc
                                    select a).FirstOrDefault();

                            cboCell.DataSource = GetAllTaiKhoanKeToan();
                            cboCell.DisplayMember = "MaSoTK";
                            cboCell.ValueMember = "TenTK";
                            cboCell.Value = tkkt.TenTK;
                            row.Cells.Add(cboCell);

                            // Column TenTK
                            cell = new DataGridViewTextBoxCell();
                            cell.Value = tkkt.TenTK;
                            row.Cells.Add(cell);

                            // Column LoaiDoiTuong and DoiTuong
                            lstKhachHang = (from c in GetAllKhachHang()
                                            where c.Ten == lstButToanSecond[i].MaSoKhachHang
                                            select c).ToList();

                            lstNhaCungCap = (from s in GetAllNhaCungCap()
                                             where s.TenNhaCungCap == lstButToanSecond[i].MaSoKhachHang
                                             select s).ToList();

                            lstNhanVien = (from e in GetAllNhanVien()
                                           where e.HoTen == lstButToanSecond[i].MaSoKhachHang
                                           select e).ToList();

                            lstChiNhanhCongTy = (from p in GetAllChiNhanhCongTy()
                                                 where p.TenChiNhanh == lstButToanSecond[i].MaSoKhachHang
                                                 select p).ToList();

                            cboCellLoaiDT = new DataGridViewComboBoxCell();
                            cboCellDT = new DataGridViewComboBoxCell();

                            cboCellLoaiDT.Items.Add("");
                            cboCellLoaiDT.Items.Add("Khách hàng");
                            cboCellLoaiDT.Items.Add("Nhà cung cấp");
                            cboCellLoaiDT.Items.Add("Nhân viên");
                            cboCellLoaiDT.Items.Add("Nội bộ");

                            if (lstKhachHang.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Khách hàng";

                                cboCellDT.DataSource = GetAllKhachHang();
                                cboCellDT.DisplayMember = "Ten";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstKhachHang[0].Id;
                            }
                            else if (lstNhaCungCap.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Nhà cung cấp";

                                cboCellDT.DataSource = GetAllNhaCungCap();
                                cboCellDT.DisplayMember = "TenNhaCungCap";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstNhaCungCap[0].Id;
                            }
                            else if (lstNhanVien.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Nhân viên";

                                cboCellDT.DataSource = GetAllNhanVien();
                                cboCellDT.DisplayMember = "HoTen";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstNhanVien[0].Id;
                            }
                            else if (lstChiNhanhCongTy.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Nội bộ";

                                cboCellDT.DataSource = GetAllChiNhanhCongTy();
                                cboCellDT.DisplayMember = "TenChiNhanh";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstChiNhanhCongTy[0].Id;
                            }

                            row.Cells.Add(cboCellLoaiDT);
                            row.Cells.Add(cboCellDT);

                            // Column SoTien
                            cell = new DataGridViewTextBoxCell();
                            cell.Value = lstButToanSecond[i].GhiCo;
                            row.Cells.Add(cell);

                            // Column Id
                            cell = new DataGridViewTextBoxCell();
                            cell.Value = lstButToanSecond[i].Id;
                            row.Cells.Add(cell);

                            dgvCo.Rows.Add(row);
                        }
                        #endregion
                    }
                    // only one TKGoc in second list
                    // and binding first list to datagrid No
                    else if (lstButToanSecond[0].TKGoc == lstButToanSecond[1].TKGoc && lstButToanSecond[0].TKDoiUng != lstButToanSecond[1].TKDoiUng)
                    {
                        // ****************************
                        // ** Binding to datagrid Co **
                        // ****************************
                        #region Set value for columns and add to datagrid
                        DataGridViewRow row = new DataGridViewRow();

                        // Column SoHieuTK
                        DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
                        TaiKhoanKeToan tkkt = (from a in GetAllTaiKhoanKeToan()
                                               where a.MaSoTK == lstButToanSecond[0].TKGoc
                                               select a).FirstOrDefault();

                        cboCell.DataSource = GetAllTaiKhoanKeToan();
                        cboCell.DisplayMember = "MaSoTK";
                        cboCell.ValueMember = "TenTK";
                        cboCell.Value = tkkt.TenTK;
                        row.Cells.Add(cboCell);

                        // Column TenTK
                        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
                        cell.Value = tkkt.TenTK;
                        row.Cells.Add(cell);

                        // Column LoaiDoiTuong and DoiTuong
                        List<KhachHang> lstKhachHang = (from c in GetAllKhachHang()
                                                        where c.Ten == lstButToanSecond[0].MaSoKhachHang
                                                        select c).ToList();

                        List<NhaCungCap> lstNhaCungCap = (from s in GetAllNhaCungCap()
                                                          where s.TenNhaCungCap == lstButToanSecond[0].MaSoKhachHang
                                                          select s).ToList();

                        List<NhanVien> lstNhanVien = (from e in GetAllNhanVien()
                                                      where e.HoTen == lstButToanSecond[0].MaSoKhachHang
                                                      select e).ToList();

                        List<ChiNhanhCongTy> lstChiNhanhCongTy = (from p in GetAllChiNhanhCongTy()
                                                                  where p.TenChiNhanh == lstButToanSecond[0].MaSoKhachHang
                                                                  select p).ToList();

                        DataGridViewComboBoxCell cboCellLoaiDT = new DataGridViewComboBoxCell();
                        DataGridViewComboBoxCell cboCellDT = new DataGridViewComboBoxCell();

                        cboCellLoaiDT.Items.Add("");
                        cboCellLoaiDT.Items.Add("Khách hàng");
                        cboCellLoaiDT.Items.Add("Nhà cung cấp");
                        cboCellLoaiDT.Items.Add("Nhân viên");
                        cboCellLoaiDT.Items.Add("Nội bộ");

                        if (lstKhachHang.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Khách hàng";

                            cboCellDT.DataSource = GetAllKhachHang();
                            cboCellDT.DisplayMember = "Ten";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstKhachHang[0].Id;
                        }
                        else if (lstNhaCungCap.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Nhà cung cấp";

                            cboCellDT.DataSource = GetAllNhaCungCap();
                            cboCellDT.DisplayMember = "TenNhaCungCap";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstNhaCungCap[0].Id;
                        }
                        else if (lstNhanVien.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Nhân viên";

                            cboCellDT.DataSource = GetAllNhanVien();
                            cboCellDT.DisplayMember = "HoTen";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstNhanVien[0].Id;
                        }
                        else if (lstChiNhanhCongTy.Count > 0)
                        {
                            cboCellLoaiDT.Value = "Nội bộ";

                            cboCellDT.DataSource = GetAllChiNhanhCongTy();
                            cboCellDT.DisplayMember = "TenChiNhanh";
                            cboCellDT.ValueMember = "Id";
                            cboCellDT.Value = lstChiNhanhCongTy[0].Id;
                        }

                        row.Cells.Add(cboCellLoaiDT);
                        row.Cells.Add(cboCellDT);

                        // Column SoTien
                        cell = new DataGridViewTextBoxCell();

                        long lSoTien = 0;
                        for (int i = 0; i < lstButToanSecond.Count; i++)
                            lSoTien += lstButToanSecond[i].GhiCo;

                        cell.Value = lSoTien;
                        row.Cells.Add(cell);

                        // Column Id
                        cell = new DataGridViewTextBoxCell();
                        cell.Value = lstButToanSecond[0].Id;
                        row.Cells.Add(cell);

                        dgvCo.Rows.Add(row);
                        #endregion


                        // ****************************
                        // ** Binding to datagrid No **
                        // ****************************
                        #region Set value for columns and add to datagrid
                        for (int i = 0; i < lstButToanFirst.Count; i++)
                        {
                            row = new DataGridViewRow();

                            // Column SoHieuTK
                            cboCell = new DataGridViewComboBoxCell();
                            tkkt = (from a in GetAllTaiKhoanKeToan()
                                    where a.MaSoTK == lstButToanFirst[i].TKGoc
                                    select a).FirstOrDefault();

                            cboCell.DataSource = GetAllTaiKhoanKeToan();
                            cboCell.DisplayMember = "MaSoTK";
                            cboCell.ValueMember = "TenTK";
                            cboCell.Value = tkkt.TenTK;
                            row.Cells.Add(cboCell);

                            // Column TenTK
                            cell = new DataGridViewTextBoxCell();
                            cell.Value = tkkt.TenTK;
                            row.Cells.Add(cell);

                            // Column LoaiDoiTuong and DoiTuong
                            lstKhachHang = (from c in GetAllKhachHang()
                                            where c.Ten == lstButToanFirst[i].MaSoKhachHang
                                            select c).ToList();

                            lstNhaCungCap = (from s in GetAllNhaCungCap()
                                             where s.TenNhaCungCap == lstButToanFirst[i].MaSoKhachHang
                                             select s).ToList();

                            lstNhanVien = (from e in GetAllNhanVien()
                                           where e.HoTen == lstButToanFirst[i].MaSoKhachHang
                                           select e).ToList();

                            lstChiNhanhCongTy = (from p in GetAllChiNhanhCongTy()
                                                 where p.TenChiNhanh == lstButToanFirst[i].MaSoKhachHang
                                                 select p).ToList();

                            cboCellLoaiDT = new DataGridViewComboBoxCell();
                            cboCellDT = new DataGridViewComboBoxCell();

                            cboCellLoaiDT.Items.Add("");
                            cboCellLoaiDT.Items.Add("Khách hàng");
                            cboCellLoaiDT.Items.Add("Nhà cung cấp");
                            cboCellLoaiDT.Items.Add("Nhân viên");
                            cboCellLoaiDT.Items.Add("Nội bộ");

                            if (lstKhachHang.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Khách hàng";

                                cboCellDT.DataSource = GetAllKhachHang();
                                cboCellDT.DisplayMember = "Ten";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstKhachHang[0].Id;
                            }
                            else if (lstNhaCungCap.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Nhà cung cấp";

                                cboCellDT.DataSource = GetAllNhaCungCap();
                                cboCellDT.DisplayMember = "TenNhaCungCap";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstNhaCungCap[0].Id;
                            }
                            else if (lstNhanVien.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Nhân viên";

                                cboCellDT.DataSource = GetAllNhanVien();
                                cboCellDT.DisplayMember = "HoTen";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstNhanVien[0].Id;
                            }
                            else if (lstChiNhanhCongTy.Count > 0)
                            {
                                cboCellLoaiDT.Value = "Nội bộ";

                                cboCellDT.DataSource = GetAllChiNhanhCongTy();
                                cboCellDT.DisplayMember = "TenChiNhanh";
                                cboCellDT.ValueMember = "Id";
                                cboCellDT.Value = lstChiNhanhCongTy[0].Id;
                            }

                            row.Cells.Add(cboCellLoaiDT);
                            row.Cells.Add(cboCellDT);

                            // Column SoTien
                            cell = new DataGridViewTextBoxCell();
                            cell.Value = lstButToanFirst[i].GhiNo;
                            row.Cells.Add(cell);

                            // Column Id
                            cell = new DataGridViewTextBoxCell();
                            cell.Value = lstButToanFirst[i].Id;
                            row.Cells.Add(cell);

                            dgvNo.Rows.Add(row);
                        }
                        #endregion
                    }
                }
                    #endregion
            }
            #endregion
        }

        private void cboPrefix_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (prefixOld != ((BangMaPhieu)cboPrefix.SelectedItem).MaSo)
                txtMaSoChungTu.Text = cboPrefix.Text == "" ? "" : KiemTraMaSo.CheckCode(((BangMaPhieu)cboPrefix.SelectedItem).MaSo, idLoaiPhieu, dtpNgayLap.Value);
        }

        private void dtpNgayLap_ValueChanged(object sender, EventArgs e)
        {
            if (dtOld != dtpNgayLap.Value)
                txtMaSoChungTu.Text = cboPrefix.Text == "" ? "" : KiemTraMaSo.CheckCode(((BangMaPhieu)cboPrefix.SelectedItem).MaSo, idLoaiPhieu, dtpNgayLap.Value);
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvNo.CurrentRow != null)
            {
                if (dgvNo.CurrentRow.Index != (dgvNo.Rows.Count - 1))
                    dgvNo.Rows.RemoveAt(dgvNo.CurrentRow.Index);
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (dgvCo.CurrentRow != null)
            {
                if (dgvCo.CurrentRow.Index != (dgvCo.Rows.Count - 1))
                    dgvCo.Rows.RemoveAt(dgvCo.CurrentRow.Index);
            }
        }

        private void dgvNo_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvNo.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvNo.ClearSelection();
                    dgvNo.CurrentCell = dgvNo.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvNo.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }

        private void dgvCo_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvCo.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvCo.ClearSelection();
                    dgvCo.CurrentCell = dgvCo.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvCo.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }

        private void dgvNo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            }
        }

        private void dgvCo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewComboBoxEditingControl)
            {
                ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
                ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
                ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
            }
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;

            return listChiNhanhCongTy;
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;

            return listKhachHang;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> listNhaCungCap = (List<NhaCungCap>)resultObj.Result;

            return listNhaCungCap;
        }

        public List<NhanVien> GetAllNhanVien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhanvienMgt.GetAllNhanVien();
            List<NhanVien> listNhanVien = (List<NhanVien>)resultObj.Result;

            return listNhanVien;
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            List<TaiKhoanKeToan> lstTaiKhoanKeToan = new List<TaiKhoanKeToan>();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            lstTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return lstTaiKhoanKeToan;
        }

        public List<MucChi> GetAllMucChi()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = mucchiMgt.GetAllMucChi();
            List<MucChi> listMucChi = (List<MucChi>)resultObj.Result;

            return listMucChi;
        }

        public List<BangMaPhieu> GetAllBangMaPhieu(int id)
        {
            ResultObj resultObj = new ResultObj();

            resultObj = bangmaphieuMgt.GetAllBangMaPhieuByIDLoaiPhieu(id);
            List<BangMaPhieu> listBangMaPhieu = (List<BangMaPhieu>)resultObj.Result;

            return listBangMaPhieu;
        }
    }
}
